<?php


namespace api\app\controller;


use api\app\model\MemberModel;
use api\app\validate\MemberValidate;
use cmf\controller\RestBaseController;

class LoginController extends RestBaseController
{
    public function login()
    {
        $data = input('post.');
        $validate = new MemberValidate();
        $res = $validate->scene('login')->check($data);
        if (!$res) {
            $this->error($validate->getError());
        }
        $member = MemberModel::where('mobile','=',$data['mobile'])->find();
        if (!$member) {
            $this->error('该手机号未注册');
        }
        if ($member['password'] != md5($data['password'] .  $member['salt'])) {
            $this->error('密码不正确');
        }
        $token = CreateToken(
            [
                'user_id' => $member['id'],
                'mobile' => $member['mobile']
            ]
        );
        $this->success('登录成功',[
            'token' => $token
        ]);
    }

}